package com.crm.config;

import java.util.Map;
import java.util.Set;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import com.alibaba.fastjson.JSONArray;
import com.crm.services.ContactServices;
import com.crm.services.CustomerServices;
import com.crm.services.DictionaryServices;
import com.crm.services.SalesOpportunityServices;
import com.crm.services.UserServices;

/**
 * 
 * @author 飞鱼
 *
 */
@Configuration
public class SalesInterceptor implements HandlerInterceptor, WebMvcConfigurer {

	@Autowired
	private CustomerServices cus;
	@Autowired
	private UserServices us;
	@Autowired
	private DictionaryServices ds;
	@Autowired
	private ContactServices cos;

	/*
	 * 预处理 在Controller的处理方法调用之前 如果返回false，则不再进入Controller，流程会被中断 request ：请求对象
	 * response：响应对象 obj：代表当前的Controller的方法（HandlerMethod）
	 */
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {

		return true;
	}

	/*
	 * 在Controller方法执行之后，创建ModelAndView之后，视图渲染之前拦截
	 */
	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
		// TODO Auto-generated method stub
	}

	/*
	 * 视图渲染完毕，一般用于收尾操作
	 */
	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
		// TODO Auto-generated method stub
		removeThreadLocalInfo();

	}

	/*
	 * 销售机会新增的地址 假设请求http://localhost:8080/emp/list/1002。如果没有对应的session对象，则会默认转到首页
	 */
	public void addInterceptors(InterceptorRegistry registry) {
		System.out.println("销售机会新增的地址拦截。。。。。");
		registry.addInterceptor(this)
				// 拦截的地址
				.addPathPatterns("/sales/Addsale");
		// 拦截排除的地址
		// .excludePathPatterns("/", "/login", "/error","/js/**","/css/**","/imgs/**");
	}

	public void removeThreadLocalInfo() {
		RequestHolder.remove();
	}
}
